package com.wrench.elevator.base.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wrench.elevator.base.user.dao.WalletDao;
import com.wrench.elevator.base.user.domain.Wallet;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description: WalletService
 * @author yangchenfeng
 * @date Oct 17, 2017 1:27:48 PM
 * @version V1.0
 */
@Component
public class WalletService
{
    @Resource
    private WalletDao dao;
    
    @Resource
    private FlowService flowService;
    
    private static final Logger logger = LoggerFactory.getLogger(WalletService.class);
    
    public int addWallet(int user_id)
        throws WrenchException
    {
        Wallet wallet = new Wallet();
        wallet.setBalance(0);
        wallet.setUser_id(user_id);
        
        int commit = -1;
        try
        {
            commit = dao.addWallet(wallet);
        }
        catch (Exception e)
        {
            logger.error("==> commit=dao.addWallet(wallet) execute error , user_id = {} , error = {} ", user_id, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.addWallet(wallet) execute success , but commit <=0 , commit = {} , user_id = {} ",
                commit,
                user_id);
            throw new WrenchException(Result.WALLET_ADD_WALLET_FAIL_CODE, Result.WALLET_ADD_WALLET_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.addWallet(wallet) execute success , user_id = {} , commit = {} ", user_id, commit);
        
        return commit;
    }
    
    public int recharge(String money, String user_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(money, user_id))
        {
            logger.error("==> param is not found , money = {} , user_id = {} ", money, user_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断user_id是不是整数
        if (!WrenchUtil.isInt(user_id))
        {
            
            logger.error("==> string is not int , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
            
        }
        // 判断money是不是金额的格式 整数或在两位以下小数
        
        if (!WrenchUtil.isMoneyStr(money))
        {
            logger.error("==> string is not money  , money = {} ", money);
            throw new WrenchException(Result.STRING_IS_NOT_MONEY_CODE, Result.STRING_IS_NOT_MONEY_MESSAGE);
        }
        
        
        int commit = -1;
        try
        {
            commit = dao.recharge(Double.valueOf(money), Integer.valueOf(user_id));
        }
        catch (Exception e)
        {
            logger.error("==>  commit=dao.recharge(Double.valueOf(money), Integer.valueOf(user_id)) execute error , money = {} , user_id = {} , error = {}; ",
                money,
                user_id,
                e);
            throw new WrenchException(Result.DB_UPDATE_ERROR_CODE, Result.DB_UPDATE_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.recharge(Double.valueOf(money), Integer.valueOf(user_id)) execute success , but commit <=0 , commit = {} , money = {} , user_id = {}  ",
                commit,
                money,
                user_id);
            throw new WrenchException(Result.WALLET_RECHARGE_FAIL_CODE, Result.WALLET_RECHARGE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.recharge(Double.valueOf(money), Integer.valueOf(user_id)) execute success , commit = {} , money = {} , user_id = {}  ",
            commit,
            money,
            user_id);
        
        return commit;
        
    }
    
    public int consume(String money, String user_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(money, user_id))
        {
            logger.error("==> param is not found , money = {} , user_id = {} ", money, user_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断user_id是不是整数
        if (!WrenchUtil.isInt(user_id))
        {
            
            logger.error("==> string is not int , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
            
        }
        // 判断money是不是金额的格式 整数或在两位以下小数
        
        if (!WrenchUtil.isMoneyStr(money))
        {
            logger.error("==> string is not money  , money = {} ", money);
            throw new WrenchException(Result.STRING_IS_NOT_MONEY_CODE, Result.STRING_IS_NOT_MONEY_MESSAGE);
        }
        
        int commit = -1;
        try
        {
            commit = dao.consume(Double.valueOf(money), Integer.valueOf(user_id));
        }
        catch (Exception e)
        {
            logger.error("==>  commit=dao.consume(Double.valueOf(money), Integer.valueOf(user_id)) execute error , money = {} , user_id = {} , error = {}; ",
                money,
                user_id,
                e);
            throw new WrenchException(Result.DB_UPDATE_ERROR_CODE, Result.DB_UPDATE_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.consume(Double.valueOf(money), Integer.valueOf(user_id)) execute success , but commit <=0 , commit = {} , money = {} , user_id = {}  ",
                commit,
                money,
                user_id);
            throw new WrenchException(Result.WALLET_CONSUME_FAIL_CODE, Result.WALLET_CONSUME_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.consume(Double.valueOf(money), Integer.valueOf(user_id)) execute success , commit = {} , money = {} , user_id = {}  ",
            commit,
            money,
            user_id);
        
        return commit;
        
    }
}
